#include "client.h"
#define MAXSIZE 1024
int clientfd_all[2] = {0};
int pipefds[2];
char login_name[50] = {0};
void sig_handler(int sig){
    //向server发送退出信息，通知server取出userlist节点
    train_t t;
    memset(&t,0,sizeof(t));
    t.len = 0;
    t.type = CMD_TYPE_QUIT;
    sendn(clientfd_all[0],&t,8+t.len);
    sendn(clientfd_all[1],&t,8+t.len);
    exit(1);
}

int main(int argc,char* argv[])
{
    char ip[50] ="192.168.50.128";
    char port1[10] = "8080";
    char port2[10] = "9999";
    int clientfd = tcpinit(ip,port1);
    clientfd_all[0] = clientfd;//记录通信fd
    signal(SIGINT,sig_handler);
    //选择登录/注册进入
    while(1){
        printf("please choose to register/login:");
        char model[20] = {0};
        scanf("%s",model);
        if(!strcmp(model , "register")){
            user_register(clientfd);
        }else if(!strcmp(model , "login")){
            break;
        }
    }
    //创建子线程，并绑定新的端口号
    pthread_t tid;
    //用管道实现主线程通知子线程
    if(pipe(pipefds)< 0 ){
        perror("pipe failed");
        exit(EXIT_FAILURE);
    }
    //设置线程的传入参数
    thread_task_t t_task;
    memset(&t_task,0,sizeof(t_task));
    strcpy(t_task.ip,ip);
    strcpy(t_task.port,port2);
    t_task.pipefd = pipefds[0];

    if((pthread_create(&tid,NULL,thread_func,&t_task)) != 0){
        perror("pipe failed");
        exit(EXIT_FAILURE);
    }

    login(clientfd);
    const char* commands[9] = {     // 字符串数组
        "pwd",
        "ls",
        "cd",
        "mkdir",
        "rmdir",
        "puts",
        "gets",
        "tree",
        "quit"
    };
    showmenu();
    //执行命令
    while (1) {
        //创建一个任务类型
        client_task_t task;
        memset(&task,0,sizeof(task));
        task.pipefd = pipefds[1];
        task.peerfd  = clientfd;
    //初始化client端的任务client_task_t
        char buff[MAXSIZE] = {0};
        //fgets(buff, MAXSIZE, stdin);
        read(STDIN_FILENO, buff, MAXSIZE);
        //命令格式为：如cd dir
        char *bufhead = NULL,*bufbody = NULL;
        parseInput(buff, &bufhead, &bufbody);
        for(int i =0;bufbody && bufbody[i]!='\0';i++){
            task.train.buff[i] = bufbody[i];
        }
        // 遍历字符串数组并比较
        int i;
        for (i = 0; i < 9; i++) {
            if (strcmp(commands[i], bufhead) == 0) {
                task.train.type = (CmdType)(i + 1);
                break;
            }
        }
        // 如果循环结束后还没有找到匹配项，说明输入的错误的命令
        if (i == 9) {
            task.train.type = CMD_TYPE_NOTCMD;
        }
        //处理不同的任务类型
        switch (task.train.type) {
        case CMD_TYPE_PWD :
            pwdCommand(&task);
            break;
        case CMD_TYPE_LS:
            // 处理 ls 命令
            lsCommand(&task);
            break;
        case CMD_TYPE_CD:{
            task.train.len = strlen(task.train.buff);
            int ret = sendn(task.peerfd, &task.train, 4 + 4 + task.train.len);
            if (ret == -1) {//出错
                printf("sendn");
            }
            // 处理 cd 命令
            printf("Changing directory ...\n");
            break;}
        case CMD_TYPE_MKDIR:{
            task.train.len = strlen(task.train.buff);
            int ret = sendn(task.peerfd, &task.train, 4 + 4 + task.train.len);
            if (ret == -1) {//出错
                printf("sendn");
            }
            // 处理 mkdir 命令
            /* mkdirCommand(task); */
            printf("Creating directory ...\n");
            break;}
        case CMD_TYPE_RMDIR:{
            task.train.len = strlen(task.train.buff);
            int ret = sendn(task.peerfd, &task.train, 4 + 4 + task.train.len);
            if (ret == -1) {//出错
                printf("sendn");
            }
            // 处理 rmdir 命令
            /* rmdirCommand(task); */
            printf("Removing directory ...\n");
            break;}
        case CMD_TYPE_PUTS:{
            task.train.len = strlen(task.train.buff);
            printf("进入命令后小火车的长度是%d\n",task.train.len);
            write(task.pipefd,&task.train,8+task.train.len);
            printf("send %s over\n", task.train.buff);
            continue;}
        case CMD_TYPE_GETS:{
            task.train.len = strlen(task.train.buff);
            printf("进入命令后小火车的长度是%d\n",task.train.len);
            write(task.pipefd,&(task.train),8+task.train.len);
            printf("Receiving file ...\n");
            continue;}
        case CMD_TYPE_TREE:
            // 处理 tree 命令
            treeCommand(&task);
            printf("Receiving tree ...\n");
            break;
        case CMD_TYPE_QUIT:
            pthread_join(tid,NULL);
            exit(1);
        default:
            // 可选：处理未预期的命令类型
            printf("Unhandled command type.\n");
            break;
        }
        
    }//while
    return 0;
}

